﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;

using System.Text;
using System.Linq.Expressions;

using System.IO;
using System.Windows.Forms;


namespace Vparser
{

    public class stk_ty { public string oper; public char prfx; public char typ; public vMod.vvbl v_nm; public stk_ty nxt; }

    public class vcond { public List<stk_ty> conds; public int vtix; public int vfix; }

  //  public class meref { }

    public class valb
    {
        string op = null;
        uint bp = 0, np = 0;
        uint v1;
        uint v2;
        int nxtprec;
        enum vprec
        {
            //Not = 11, Mult = 10, Dvd = 10, Modo = 10, Plus = 9, Minus = 9, Ls = 8, Rs = 8,
            //L = 7, LE = 7, G = 7, GE = 7, E = 6, RE = 6, NE = 6, Ident = 5, Nident = 5, and = 4, nand = 4,
            //XO = 3, XON = 3, Or = 2, NOR = 2, cAnd = 1, cOr = 1, 
            MD = 10, AS = 9, LRS = 8, LGE = 7, EE_NE = 6, ID = 5, A = 4, NA = 4, XO = 3, O = 2, NO = 2, NN = 1, OO = 1

        }

    

        public uint vv(int prec, stk_ty sx) //List<stk_ty> vL, int prec, ref int ox, stk_ty sx)
        {
          

            v1 = sx.v_nm.val; 
            if(sx.nxt == null)
            {
                if (sx.prfx == '!')
                    if (v1 > 0) v1 = 0;
                    else v1 = 1;
                if (sx.prfx == '~')
                    return ~v1;
                return v1;
            }

            for (; sx != null; sx = sx.nxt) 
            {
                //if ((op = sx.oper) != null)
                //{
                //}
                if (op == ")" || nxtprec < prec) return v1;

                switch (sx.oper)
                {
                    case "&":
                        //if ((nxtprec = (int)vprec.A) > prec)
                        //    v1 &= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.A;
                        v1 &= sx.nxt.v_nm.val;
                        break;
                    case "|":
                        //if ((nxtprec = (int)vprec.O) > prec)
                        //    v1 |= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.O;
                        v1 |= sx.nxt.v_nm.val;
                        break;
                    case "^":
                        //if ((nxtprec = (int)vprec.XO) > prec)
                        //    v1 ^= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.XO;
                        v1 ^= sx.nxt.v_nm.val;
                        break;
                    case "+":
                        //if ((nxtprec = (int)vprec.AS) > prec)
                        //    v1 += vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.AS;
                        v1 += sx.nxt.v_nm.val;  //vL[ox]
                        break;
                    case "-":
                        //if ((nxtprec = (int)vprec.AS) > prec)
                        //    v1 -= vv(ref vL, nxtprec, ref ox);
                        //else

                        prec = (int)vprec.AS;
                        v1 -= sx.nxt.v_nm.val;
                        break;
                    case "~&":
                        //if ((nxtprec = (int)vprec.NA) > prec)
                        //    v1 = ~(v1 &= vv(ref vL, nxtprec, ref ox));
                        //else
                        prec = (int)vprec.NA;
                        v1 = ~(v1 & sx.nxt.v_nm.val);
                        break;
                    case "~|":
                        //if ((nxtprec = (int)vprec.O) > prec)
                        //    v1 = ~(v1 | vv(ref vL, nxtprec, ref ox));
                        //else
                        prec = (int)vprec.NO;
                        v1 = ~(v1 | sx.nxt.v_nm.val);
                        break;
                    case "^~":
                        //if ((nxtprec = (int)vprec.XO) > prec)
                        //    v1 = ~(v1 ^ vv(ref vL, nxtprec, ref ox));
                        //else
                        prec = (int)vprec.XO;
                        v1 = ~(sx.nxt.v_nm.val);
                        break;
                    case "&&":
                        if ((v1 > 0) && (sx.nxt.v_nm.val > 0))
                            return 1;
                        else
                            return 0;
                    case "||":
                        if ((v1 > 0) || (sx.nxt.v_nm.val > 0))
                            return 1;
                        else
                            return 0;
                    case "<":
                        if (v1 < sx.nxt.v_nm.val)
                            return 1;
                        else
                            return 0;
                    case "<=":
                        if (v1 <= sx.nxt.v_nm.val)
                            return 1;
                        else
                            return 0;
                    //case "=":
                    //    if (v1 == v2)
                    //        return 1;
                    //    else
                    //        return 0;
                    case "==":
                        if (v1 == (sx.nxt.v_nm.val))
                            return 1;
                        else
                            return 0;
                    case ">":
                        if (v1 > sx.nxt.v_nm.val)
                            return 1;
                        break;
                    case ">=":
                        if (v1 >= sx.nxt.v_nm.val)
                            return 1;
                        else
                            return 0;
                    case "*":
                        //if ((nxtprec = (int)vprec.MD) > prec)
                        //    v1 *= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.MD;
                        v1 *= sx.nxt.v_nm.val;
                        break;
                    case "/":
                        //if ((nxtprec = (int)vprec.MD) > prec)
                        //    v1 /= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.MD;
                        v1 /= sx.nxt.v_nm.val;
                        break;
                    case "%":
                        //if ((nxtprec = (int)vprec.MD) > prec)
                        //    v1 %= vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.MD;
                        v1 %= sx.nxt.v_nm.val;
                        break;
                    case "<<":
                        //if ((nxtprec = (int)vprec.LRS) > prec)
                        //    v1 <<= (int)vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.LRS;
                        v1 <<= (int)vval(sx.nxt.v_nm.val);
                        break;
                    case ">>":
                        //if ((nxtprec = (int)vprec.LRS) > prec)
                        //    v1 >>= (int)vv(ref vL, nxtprec, ref ox);
                        //else
                        prec = (int)vprec.LRS;
                        v1 >>= (int)vval(sx.nxt.v_nm.val);
                        break;
                    case "===":
                        break;
                    case "!==":
                        break;
                    case null:
                        return v1;
                        break;
                    default:
                        MessageBox.Show("Missed op ");
                        break;
                }
            }
            return v1;
        }  // end vv

        uint vval(uint vix)
        {
            // if(oper == "(" || (nxtprec > prec) return vv()
            // else return .val
            return 0;
        }

    }

    //public class simscan
    //{
    //    public void myscan(ref tb sx)
    //    {
    //        scan(ref sx);
    //    }
    //}


    public class tb
    {
        public string name;
        public char nm_typ;
        public uint sc, sv, bv1, bv2, ix, val, scn_ix;
        public vMod.vvbl dvbl;
        public tb nxt;
        public
        long seek_ch;
    }
        
public  class vParse
    {
        FileStream fs_in; // = new FileStream(fPath, FileMode.Open);
        StreamReader sr_in;  // = new StreamReader(fs_in);
        BinaryReader br_in;  // = new BinaryReader(fs_in);
        List<tb> events = new List<tb>(1000);
        List<stk_ty> clk = new List<stk_ty>();
        public struct vals { public uint time; public uint val;};
        
        public List<stk_ty> OpL;
        public List<vMod.ops> bL = new List<vMod.ops>();
        public List<vMod.ops> nbL = new List<vMod.ops>();
        vMod.vif vpif = new vMod.vif();
        int lineno;
        bool end_flag = false;
        string[] sScan;
        string sIn;
        [Flags]
        public enum bit
        {
            b0 = 0x1, b1 = 0x2, b2 = 0x4, b3 = 0x8,
            b4 = 0x10, b5 = 0x20, b6 = 0x40, b7 = 0x80
        }
        public vMod newMod;
        vBuild vSys;
        public valb vb = new valb();
        public vParse vpo2;
    //    public vMod.vif vpif = new vMod.vif();
        public struct output
        {
            public string name; public vMod.vvbl vbl; //public List<stk_ty> opL;
            public List<vMod.input> fout;
                // public int ix; public int fx; public int fy;public string vtyp;  
        }

        vMod.valways valS;
        vMod.vif vIf;
   // public 
//        Operator Precedence 
//+, -, !, ~ (unary) Highest 
//*, /, %  
//+, - (binary)  
//<<, >>  
//<, <=, >, >=  
//=, ==, !=  
//===, !==  
//&, ~&  
//^, ^~  
//|, ~|  
//&&  
//||  
//?: Lowest 
enum vprec
{
    //Not = 11, Mult = 10, Dvd = 10, Modo = 10, Plus = 9, Minus = 9, Ls = 8, Rs = 8,
    //L = 7, LE = 7, G = 7, GE = 7, E = 6, RE = 6, NE = 6, Ident = 5, Nident = 5, and = 4, nand = 4,
    //XO = 3, XON = 3, Or = 2, NOR = 2, cAnd = 1, cOr = 1, 
    MD = 10, AS = 9, LRS = 8, LGE = 7, EE_NE = 6, ID = 5, A = 4, NA = 4, XO = 3, O = 2, NO = 2, NN = 1, OO = 1

}
char[] copers1 = new char[] { '+', '-', '!', '~', '=', '?', ':', '&', '|', '^', '*', '/', '<', '>', '%' };
char[] copers2 = new char[] { '~', '=', '&', '|', '^' };
    string[] sopers = new string[] {"+", "-", "!", "~", "*", "/", "%", "<<", ">>", "<", "<=", ">",
            ">=" ,"=", "==", "!=", "===", "!==", "&", "~&", "^", "~^", "|", "~|", "&&", "||", "?", ":"};
        char[] wsp = new char[] { };
        string vexp = null;
        ListBox iL;
        ListBox lB2;
        ListBox lB3;
        vParse vpo;
        //List<vMod.vvbl> bL;  // = new List<vMod.vvbl>();
        //List<vMod.vvbl> nbL;  // = new List<vMod.vvbl>();
    string token;
    string sRelb = "";
    string portlist;
    char[] d3 = new char[] { '(', '=', '@' };
    int tx;
    int lp, rp, pcnt = 0;
    int vwidth = 0;
    int nxtprec;
    uint rn_tm = 0, simtm = 0;
    tb tm_org = new tb();
    int scn_tm;

    FileStream log_out;
    StreamWriter scn_log;
    StreamReader log_in;
    tb prntx;
    struct scrn { public string ln; public vMod.vvbl vbl; public int ix; public int tm;
                  public long seek_pos; public List<vals> trace;
    }
    List<scrn> scn = new List<scrn>();
    scrn s_p;
    tb[] pid = new tb[1000];
//    class evbl : vMod.vvbl { }


    string op = null;
    uint bp = 0, np = 0, vwid = 0;
    uint v1;
    uint v2;
    // int nxtprec;
    //    enum vprec
    //{
    //    //Not = 11, Mult = 10, Dvd = 10, Modo = 10, Plus = 9, Minus = 9, Ls = 8, Rs = 8,
    //    //L = 7, LE = 7, G = 7, GE = 7, E = 6, RE = 6, NE = 6, Ident = 5, Nident = 5, and = 4, nand = 4,
    //    //XO = 3, XON = 3, Or = 2, NOR = 2, cAnd = 1, cOr = 1, 
    //    MD = 10, AS = 9, LRS = 8, LGE = 7, EE_NE = 6, ID = 5, A =4, NA =4, XO = 3, O = 2, NO = 2, NN = 1, OO = 1

    //}

public int Pnew(String fPath, ListBox iL2, ListBox lB2a, ListBox lB3a, vParse vpi)
        {
    
            iL = iL2;
            lB2 = lB2a;
            lB3 = lB3a;
            vpo = vpi;
            iL.Items.Add("Start");
            fs_in = new FileStream(fPath, FileMode.Open);
            sr_in = new StreamReader(fs_in);
            br_in = new BinaryReader(fs_in);
            initial(fPath);
            lineno = 0;
            vSys = new vBuild();  // vSys is control module: clocking and always events;  Global control
            vSys.modList = new List<vMod>();
            vSys.mvbls = new List<vMod.vvbl>();
            valb vb = new valb();
            //BitField bits = new BitField();  // just to try bit extraction
            //bits.get_fld(0x4321, 4, 8, 0, 0, 0);
            string[] sScana;
            string[] sScanb;
    
          //  vMod.vedge o = new vMod.vedge();
          //  vMod.vvbl ov = new vMod.vvbl();
          //  Type t = o.GetType();
          ////  vMod.vvbl ov;
          //  ov = o;
          //  o = (vMod.vedge)  ov;

        //    new delegate xd
        //xd = xOp => xOp + xT;
            uint addr = 0X80, xx, xxx, i;
            int[] bz = new int[256];
            int dot;
            for (int x = 1; x < 8; x++)
            {
                if (x == 1)
                    addr = 0X80;
                    xx = addr;
                    xxx = xx | (addr >>= 1);
                    for (i = xx; i <= xxx; i++)
                        bz[i] = x;
            }
         //   vMod.input myin = new vMod.input();
         //   vParse.output myo = new vParse.output();
         //   List<object> myL = new List<object>();
         //   myL.Add(myin);
         //   myL.Add(myo);
         //   Type vTi = myL[0].GetType();
         //   object oo = myL[0];
         //   vMod.input myin2 = (vMod.input)myL[0];
         //   Type vTo = myL[1].GetType();
         //   string onm = (myL[0].GetType().Name);
         ////   int jj = myin2.ix;
         //   vParse.output myo1 = (vParse.output)myL[1];
            for (NextLine(); !sr_in.EndOfStream; NextLine())
            {
                string token = vtok();
            //    vexp = get_exp(vexp);
                if (token == "module")  //sScan.Length > 1 && 
                {
                    end_flag = false;

                    if ((lp = vexp.IndexOfAny(new char[] { '#', '(', ';' })) >= 0)
                    {
                        if (lp > 0) newMod = nm_tbl(vexp.Substring(0, lp).Trim());
                        else
                        {
                            MessageBox.Show("Missing module name");
                            return -1;
                        }
                        if(newMod.mvbls == null)
                        newMod.mvbls = new List<vMod.vvbl>();
                        newMod.pvp = this;
                        do
                            switch (vexp[lp])
                            {
                                case '#':
                                    vexp = vexp.Substring(lp);
                                    if ((lp = vexp.IndexOf('#')) >= 0)
                                    {
                                        if (vexp[lp + 1] == '(')
                                        {
                                            if ((rp = vexp.IndexOf(')')) > 0)
                                                vexp = vexp.Substring(lp + 2);  //, rp);
                                            else
                                                MessageBox.Show("Missing ) in parm");
                                        }
                                        sScana = vexp.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                                        //  {

                                        if (sScana[0].Trim() == "parameter")
                                        {
                                            if (newMod.parms == null)
                                                newMod.parms = new List<vMod.parm>();
                                            sScanb = sScana[1].Split(new char[] { ',', ')' }, StringSplitOptions.RemoveEmptyEntries);
                                            vMod.parm nparm = new vMod.parm();
                                            foreach (string sp in sScanb)
                                            {
                                                int eix = sScanb[0].IndexOf('=');
                                                nparm.name = sScanb[0].Substring(0, eix).Trim();
                                                //    nparm.val = Convert.ToUInt32(sScanb[0].Substring(eix).Trim());   
                                                uint num;
                                                if (UInt32.TryParse(sScan[0].Substring(eix).Trim(), out num) == false)
                                                    MessageBox.Show("Convert error" + lineno);
                                                nparm.val = num;
                                                newMod.parms.Add(nparm);   //  name, value
                                            }
                                        }

                                        else
                                        {
                                            MessageBox.Show("missing parm");

                                        }
                                        if ((rp = vexp.IndexOf(')')) >= 0)   // do ports
                                            vexp = vexp.Substring(rp + 1).Trim();
                                    }
                                    break;
                                case '(':
                                    vexp = vexp.Substring(lp + 1);
                                    string ptyp = "";
                                    sScanb = vexp.Split(new char[] { '(', ',', ')', ';' }, StringSplitOptions.RemoveEmptyEntries);
                                    foreach (string sn in sScanb)
                                    {
                                        sScana = sn.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                                        switch (token = sScana[0])
                                        {
                                            case "input":  // inputs are wires:  need width , opl
                                                if (newMod.inputs == null) newMod.inputs = new List<vMod.input>();
                                                ptyp = token;
                                                if (sn.IndexOf('[') >= 0)
                                                {
                                                    if ((rp = sn.IndexOf(']')) > 0)
                                                    {

                                                        pnms(newMod, ptyp, sn.Substring(rp + 1).Trim());
                                                    }
                                                    else MessageBox.Show("width syntax");
                                                }
                                                else
                                                    pnms(newMod, ptyp, sScana[1].Trim());  // pnms processes mod.out
                                                break;
                                            case "output":  // can be reg or wire:  width, vbl obj
                                                if (newMod.outputs == null) newMod.outputs = new List<vParse.output>();
                                                ptyp = token;
                                                if (sn.IndexOf('[') >= 0)
                                                {
                                                    if ((rp = sn.IndexOf(']')) > 0)
                                                    {
                                                        pnms(newMod, ptyp, sn.Substring(rp + 1).Trim());
                                                    }
                                                    else MessageBox.Show("width syntax");
                                                }
                                                else
                                                    pnms(newMod, token, sScana[1].Trim());
                                                break;
                                            //case "inout":  //  ???wire only???  eidth, ??? cond assign ???

                                            //    break;
                                            default:  //  port syntax error ??
                                                pnms(newMod, ptyp, sn);
                                                break;
                                        }

                                    } // end foreach
                                    break;
                                case ';':
                                    break;
                                //else
                                //{
                                //    if ((lp = vexp.IndexOf('(')) > 0)
                                //        vexp = (vexp.Substring(lp));
                                //    portlist = vexp;  //  use this later for xref
                                //}
                                //else MessageBox.Show("Missing module name");
                                default:
                                    break;
                            } while ((lp = vexp.IndexOf('(')) >= 0);
                        newMod.vops = new List<object>();
                        NextLine();
                        listBuild(ref sScan, ref newMod.vops); // init array from list(s)
                        //Type t = newMod.vops[0].GetType();
                        //vMod.vblk vb = (vMod.vblk)newMod.vops[0];
                        //   t.bix = 9;
                    }  // end of stream
                }
            }
            return 0;
            }//  end bnew

void pnms(vMod Mod, string ptyp, string nm)
    {
    int ps = 0;
    
    string[] sScanb;
        switch (ptyp)
        {
            case "input":

                    vMod.input inSt = new vMod.input();
                sScanb = nm.Split(new char[] {'.'}, StringSplitOptions.RemoveEmptyEntries);
                inSt.name = sScanb[0].Trim(); ;
                    Mod.inputs.Add(inSt);
                    vMod.vvbl vin = add_vvbl(inSt.name);  // new vMod.vvbl();
                    inSt.vbl = vin;
                    //vin.wire = true;
                    //vin.width = vwid;
                    //inSt.vbl = vin;
                    vMod mSrc;
                    if (Mod.mvbls == null)
                    Mod.mvbls = new List<vMod.vvbl>();
                    if (sScanb.Length == 3)
                    {
                        mSrc = nm_tbl(sScanb[1]);
                        inSt.mOut = mSrc.get_out(sScanb[2], Mod, inSt);
                  //      inSt.vbl = inSt.mOut.vbl;
                        inSt.mOut.fout.Add(inSt);
                    }
                        else
                    {
                    if (sScanb.Length == 1)
                        inSt.mOut = vSys.get_out(sScanb[0].Trim() , Mod, inSt);
                    else
                        if (sScanb.Length == 2)
                        {
                            inSt.mOut = vSys.get_out(sScanb[1], Mod, inSt);
                        }
                        else MessageBox.Show(Mod.name + inSt.name + " syntax");
                   // inSt.vbl = inSt.mOut.vbl;
                    //    inSt.vbl = add_vvbl(
                    }
                            stk_ty e = new stk_ty();
                            e.v_nm = inSt.mOut.vbl;
                            inSt.vbl.wire = true;
                            inSt.vbl.opL = new List<stk_ty>();
                            inSt.vbl.opL.Add(e);
                  //  }
                 //  vMod.vvbl vin = inSt.mOut.vbl;  // new vMod.vvbl();
                 //   vin.wire = true;
                    vin.width = vwid;

                    //stk_ty e = new stk_ty();
                    //e.v_nm = inSt.mOut.vbl;
                    //vin.wire = true;
                    //vin.opL = new List<stk_ty>();
                    //vin.opL.Add(e);
                 //   inSt.vbl = vin;
                 //   Mod.mvbls.Add(vin);

             //   Mod.mvbls.Add(add_vvbl(nm));
                int dotx = 0;
                break;
            case "output":
              //  output oSt = new output();
                //oSt.name = nm;
                //oSt.fy = vwidth;
                // find src mod
                //Mod.get_out(ref   , nm);
             //   Mod.outputs.Add(oSt);
                Mod.get_out(nm, null, null);
                break;
            default:

                break;
        }
        return;

    }
void vvnms(vMod Mod, string vvtyp)
{
    int ns = 0;
    string[] nms;
    nms = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
  
            foreach (string nm in nms)
            {
              //  if (vvtyp == "reg")
                {
                    vMod.vvbl vvSt = new vMod.vvbl();
                    vvSt.name = nm.Trim();
                    if (vvtyp == "wire")
                        vvSt.wire = true;
                    newMod.mvbls.Add(vvSt);
                    iL.Items.Add(nm.Trim());
                }
            }
 
    return;

}

private bool listBuild( ref string[] sA, ref List<object> vlst  )
{
    bool rslt = true;
    //int sumbit = (int)bit.b3 + (int)bit.b4;
    string[] sS ;
    while (!sr_in.EndOfStream)
    {
        token = vtok();
     //   vexp = get_exp(vexp);
        if (token != "begin" && token != "end")
            iL.Items.Add(token);
       switch (token)
       {
           case "wire":  // find vbl obj: may be input or output, else add to wire list for assign, width, 
               string ptyp = "";
               if((rp = vexp.IndexOf(']')) >= 0)
                   vexp = vexp.Substring(rp + 1);
           sS = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
           if (newMod.mvbls == null) newMod.mvbls = new List<vMod.vvbl>();
           //  vvnms(newMod, token);
           foreach (string nm in sS)
           {
                vMod.vvbl vnw = add_vvbl(nm.Trim());
                vnw.wire = true;
           }
               break;
           case "reg":  // find vbl obj: add to reg list
                       if ((rp = vexp.IndexOf(']')) >= 0)
                           vexp = vexp.Substring(rp + 1);
                       sS = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
                 //          if (newMod.mvbls == null) newMod.mvbls = new List<vMod.vvbl>();
          //  vvnms(newMod, token);
               foreach( string nm in sS)
                   add_vvbl( nm.Trim());
                    //   }
               break;
           case "parameter":  // add to parm list: value
               if (newMod.parms == null)
                   newMod.parms = new List<vMod.parm>();
               sS = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
               foreach (string p in sS)
               {
                 //  sS = p.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                   vMod.parm nparm = new vMod.parm();
                   int eix = p.IndexOf('=');
                   nparm.name = p.Substring(0, eix++).Trim();
                //   nparm.val = Convert.ToUInt32(p.Substring(eix).Trim());
                   bool ok = UInt32.TryParse(p.Substring(eix).Trim(), out nparm.val);
                   if(UInt32.TryParse(p.Substring(eix).Trim(), out nparm.val) == false)
                       MessageBox.Show("Convert error" + lineno);
                   if (newMod.parms == null) newMod.parms = new List<vMod.parm>();
                   newMod.parms.Add(nparm);   //  name, value
                 
               }
               break;
           case "input":  // inputs are wires:  need width , vbl obj
               if (newMod.inputs == null) newMod.inputs = new List<vMod.input>();
             //  ptyp = token;
               sS = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
               foreach (string sn in sS)
               if (sn.IndexOf('[') >= 0)
               {
                   if ((rp = sn.IndexOf(']')) > 0)
                   {
                       pnms(newMod, token, sn.Substring(rp + 1).Trim());
                   }
                   else MessageBox.Show("width syntax");
               }
               else
                   pnms(newMod, token, sn.Trim());  // pnms processes mod.out
               break;
           case "output":  // can be reg or wire:  width, vbl obj
               if (newMod.outputs == null) newMod.outputs = new List<vParse.output>();
               ptyp = token;
               sS = vexp.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
               foreach (string sn in sS)
               if (sn.IndexOf('[') >= 0)
               {
                   if ((rp = sn.IndexOf(']')) > 0)
                   {
                       pnms(newMod, ptyp, sn.Substring(rp + 1).Trim());
                   }
                   else MessageBox.Show("width syntax");
               }
               else
                   pnms(newMod, token, sn.Trim());
               break;
           //case "inout":  //  ???wire only???  eidth, ??? cond assign ???

           case "initial":
              
                       sS = vexp.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                   token = vxtok(sS);
                   uint dlytm = 0;
                //   vexp = get_exp(vexp);
                   if (token == "begin")
                   {

                       sS = vexp.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                      foreach(string sinit in sS)
                       {
                           string vnm;
                           int eqx;
                           vMod.vvbl y;
                           if (sinit[0] == '$' || sinit.Trim() == "end")
                               continue;
                           else
                           {
                               tb x = new tb();
                               if (sinit.Trim()[0] == '#')
                               {
                                   sS = sinit.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                               //    dlytm += Convert.ToUInt32(sS[0].Substring(1).Trim());
                                   uint num;
                                   if (UInt32.TryParse(sS[0].Substring(1).Trim(), out num) == false)
                                       MessageBox.Show("Convert error" + lineno);
                                   dlytm += num;
                                   if (sS[1][0] != '$')
                                       vnm = sS[1].Substring(0, sS[1].IndexOf('=')).Trim();
                                   else
                                   {
                                       if(sS[1].Trim() == "$finish")
                                       simtm = dlytm;
                                       continue;
                                   }
                               }
                                  else
                                  vnm = sinit.Substring(0, sinit.IndexOf('=')).Trim();
                               eqx = sinit.IndexOf('=');
                               int dotx;
                                   if (eqx >= 0)
                                   {
                                       if (vnm[0] != '$')
                                       {
                                          dotx = vnm.IndexOf('.');
                                          if (dotx > 0)
                                          {
                                              vMod modnm = nm_tbl(vnm.Substring(0, dotx));
                                              foreach (vMod.vvbl i in modnm.mvbls)
                                                  if (i.name == vnm.Substring(dotx + 1))
                                                  {
                                                      x.dvbl = i;
                                                      x.dvbl.name = i.name;
                                                      x.dvbl.pe = i.pe;
                                                      x.dvbl.ne = i.ne;
                                                      x.dvbl.val = i.val;
                                                      break;
                                                  }
                                          }
                                          else
                                          {
                                              foreach (vMod.vvbl v in vSys.mvbls)
                                              {

                                                  if (v.name == vnm)
                                                  {
                                                      x.dvbl = v;
                                                      for (int ix = 0; ix < newMod.mvbls.Count; ix++)
                                                          if (newMod.mvbls[ix].name == vnm)
                                                          {
                                                              newMod.mvbls[ix] = v;
                                                      break;
                                                          }
                                                      break;
                                                  }
                                              }
                                              if (x.dvbl == null) MessageBox.Show("Missing init vbl");
                                          }
                                           x.bv1 = dlytm;
                                 //          x.dvbl.wire = false;  //  sets up for init value control
                                       }
                                   }
                                   else simtm = dlytm;
                               if (Char.IsDigit(sinit.Substring(eqx + 1).Trim()[0]))
                                 {
                                   //  x.val = Convert.ToUInt32(sinit.Substring(eqx + 1).Trim());
                                     //   nparm.val = Convert.ToUInt32(p.Substring(eix).Trim());
                                   //  bool ok = UInt32.TryParse(p.Substring(eix).Trim(), out x.val);
                                     if (UInt32.TryParse(sinit.Substring(eqx + 1).Trim(), out x.val) == false)
                                         MessageBox.Show("Convert error" + lineno);
                                   Addevent(ref x);
                               }
                           }
                       } //  end foreach
                   }
               break;
           default:
               while (!sr_in.EndOfStream)
               {  
                   vS();
                   sRelb = "";
                   break;
               }
               break;
       }
       NextLine();
       if (sScan[0] == "endmodule")
       {
           end_flag = true;
           MessageBox.Show("End of Module");
           if(newMod.inputs != null)
           foreach (vMod.input ipt in newMod.inputs)
           {
   //            if(ipt.vbl.opL == null)
  //             ipt.vbl = ipt.mOut.vbl;
             //  else ipt.vbl = ipt.
           }
           return rslt;
       }
    }  //  end of while
    return rslt;
}

private void vS()  //, ref bool rslt, ref string[] sS)
{
    string[] sS = new string[0];
    string[] sS2 = new string[0];
    bool begin_flag = false;
    //List<vMod.vvbl> bL = new List<vMod.vvbl>();
    //List<vMod.vvbl> nbL = new List<vMod.vvbl>();
    switch (token)
    {
      
        case "assign": // get wire obj.obj
            int eq;
            if ((eq = vexp.IndexOf('=')) < 0)
                MessageBox.Show("assign syntax");
            else
            {
                iL.Items.Add(vexp.Substring(0, eq));
               vMod.vvbl asnv = add_vvbl(vexp.Substring(0, eq).Trim());
               uint x =  asnv.val;
        //      asnv.edge = false;
               asnv.opL = get_opq(vexp.Substring(eq + 1).Trim());  // put oplist in wire vbl
            }
            break;
        case "always":  //  set up events, build statement chain, one or block
            if (vexp.IndexOf('@') < 0)
            {
                if (vexp.IndexOf('#') < 0)
                {
                    MessageBox.Show("always missing events" + token + vexp);
                    return;
                }
            }
            valS = new vMod.valways();
            valS.bL = new List<vMod.ops>();
            valS.nbL = new List<vMod.ops>();
           //  .sList.Add(valS);
            bL = valS.bL;  // if no if, blk assign goes on valways
            nbL = valS.nbL;
            valS.fif = new vMod.vif();
            vpif = valS.fif;
             vexp = always_events(ref valS);
            int scix;
            break;

        case "if":  // set up cond: build if/else/statement/block chain
            iL.Items.Add(token);
            if ((lp = vexp.IndexOf('(')) < 0) 
            // CondMethod(ref sS, ref vexp);
          //  else
            {
                MessageBox.Show("Missing if cond exp");

            }
         //   vpif = new vMod.vif();
            vIf = vpif; // valS.fif;  // 
            vIf.bL = new List<vMod.ops>();
            vIf.nbL = new List<vMod.ops>();
   
        //    vpif = vIf;
            bL = vIf.bL;  // if blk assign, put it on if
            nbL = vIf.nbL;
            CondMethod(vexp);
       //     valS.vsL.Add(vIf);
            vIf.fif = (vIf = new vMod.vif());
            vpif = vIf;
            break;
        case "else":
            vMod.velse vElse = vIf.nxtelse;  // new vMod.velse();
            valS.vsL.Add(vElse);
            vElse.bL = new List<vMod.ops>();
            vElse.nbL = new List<vMod.ops>();
            bL = vElse.bL;
            vElse.fif = new vMod.vif();
            nbL = vElse.nbL;
            //vpif.nxtelse = vElse;
            //vpif = vElse.fif;
            vSys.sList.Add(vElse);
            break;
        case "case":
        case "casex":
        case "casez":  //  create statement array based on case vbl width
            sS = vexp.Split(new char[] { '(', ')' }, 2, StringSplitOptions.RemoveEmptyEntries);
            {
                iL.Items.Add(sS[1]);
                do
                {
                    NextLine();
                    token = vtok();
                    iL.Items.Add(sIn);
                } while (token != "endcase");
                vS();
        //        iL.Items.Add(token);
            }
            break;
        case "for":  //  set up call to skeleton for method
            iL.Items.Add(sScan[0]);   //  temporary for debug
            vMod.vfor vFor = new vMod.vfor();
            vFor.op1 = 0;
            vFor.op2 = 0;
            vFor.vop = "+1";
            break;
        case "while":  // get cond:  iterate on "if"
            iL.Items.Add(vexp); 
        pcnt = 0;
        if ((lp = vexp.IndexOf('(')) >= 0)
            CondMethod(vexp); 
        else
            MessageBox.Show("Missing while cond exp");
          
            break;
        case "begin": // may not be needed
        case "begin:": //   check for "name" 
           
                begin_flag = true; 
            iL.Items.Add("begin");
        //    NextLine();
            break;
        case "end;":
        case "end":    // which statement owns this ??
            begin_flag = false;
            iL.Items.Add("end");
            return;  // reverts to calling block
        case "endcase":
            iL.Items.Add(token);   //  temporary for debug
 //           NextLine();
            break;
        default:
            if (sScan[0] == "endmodule")
                break;
            scix = vexp.IndexOf("<=");
            if (scix >= 0)
            {

                iL.Items.Add(vexp);
                vMod.ops vblt = new vMod.ops();
                vblt.vr = get_vbl(token);
                vblt.vr.edge = true;
             //   if (valS.nbL == null) valS.nbL = new List<vMod.vvbl>();
                vblt.opL = get_opq(vexp.Substring(scix + 2, vexp.IndexOf(';') - scix));              //  valS.nbL.Add(vblt);
                nbL.Add(vblt); 
            }
            else if ((scix =vexp.IndexOf('=')) >= 0)
            {
                iL.Items.Add(vexp);

          //      if (valS.bL == null) valS.bL = new List<vMod.vvbl>();
                vMod.ops vblt = new vMod.ops(); 
                vblt.vr = get_vbl(token);
                vblt.opL = get_opq(vexp.Substring(scix + 1, vexp.IndexOf(';') - scix));
                bL.Add(vblt);
            }
            vexp = vexp.Substring(vexp.IndexOf(';') + 1);
            break;
    }
}


        public string vtok()
{

    string newtok = sScan[0];
    tx = newtok.IndexOfAny(d3);
    if (tx >= 0)
    {
        vexp = get_exp(newtok.Substring(tx));
        return newtok.Substring(0,tx);
    }
    else
    {
        if (sScan.Length > 1)
            vexp = get_exp(sScan[1].Trim());
        else
        vexp = "";
        return newtok;
    }

}
        public string vxtok(string[] sS)
        {
            tx = sS[0].IndexOfAny(d3);
            if (tx >= 0)
            {
                vexp = vexp.Substring(tx);
                return sS[0].Substring(0, tx);
            }
            else
            {
                if (sS.Length > 1)
                    vexp = sS[1].Trim();
                else
                    vexp = "";
                return sS[0];
            }

        }

private void vifelse( string sRel)
{
   
    if (sRel != "else")
    {
        if (sRelb.Length != 0 && sRelb.LastIndexOf ("else") != sRelb.Length - 5)
            sRelb = sRelb + " && " + sRel;
        else
            sRelb = sRelb + " " + sRel;
    }
    else
    {
        sRelb = sRelb + " else ";
    }
    iL.Items.Add(sRelb);
    return ;
}

private void CondMethod(string sRel)  //, ref int lp, ref int pcnt, out int rp) //, out string sRels)
{
    string[] sScana;
    string sRels;
    if (token == "if")
    {
        sRel = get_parens(sRel);
        sScana = sRel.Substring(++lp, rp - lp).Split(new char[] { }, StringSplitOptions.RemoveEmptyEntries);
        sRels = String.Concat(sScana);
       vIf.condL = get_opq(sRels);
       // iL.Items.Add(sRels);
       // vifelse(sRels);
        //  vrelOp(sRels, ref iL);  //  adds cond to vif statement
        if (++rp < sRel.Length)
            vexp = sRel.Substring(rp);
        else
        {
            vexp = get_exp(null);
        }
      //  if (vexp.IndexOf('?') >= 0)
        iL.Items.Add(sRels);
        vifelse(sRels);
    }
       
    tx = 0;
    do
    {
        sScana = vexp.TrimStart().Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
        tx = sScana[0].IndexOfAny(d3);
        if (tx >= 0)
        {
            token = vexp.TrimStart().Substring(0, tx);
            vexp = vexp.TrimStart().Substring(tx);
        }
        else
        {
            if (sScana.Length > 1)
                vexp = sScana[1].TrimStart();
            else vexp = null;
            token = vxtok(sScana);
            if(token == "else")
                vIf.nxtelse = new vMod.velse();
        }
        vS();
    } while ((sScana.Length) > 1 && (vexp.Length > 0));
    NextLine();
    token = vtok();
    vexp = get_exp(null);
    if (token == "else")
    {
        vifelse("else");
        vIf.nxtelse = new vMod.velse();
        vS();
        tx = 0;
        do
        {
            sScana = vexp.TrimStart().Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
            tx = sScana[0].IndexOfAny(d3);
            if (tx >= 0)
            {
                token = vexp.TrimStart().Substring(0, tx);
                vexp = vexp.TrimStart().Substring(tx);
            }
            else
            {
                if (sScana.Length > 1)
                    vexp = sScana[1].TrimStart();
                else vexp = null;
                token = vxtok(sScana);
            }
            vS();
        } while ((sScana.Length) > 1 && (vexp.Length > 0));
        //NextLine();
        //token = vtok();
       
    }
    //  if(token == "if")
}

private string get_parens(string sRel)
{
    rp = sRel.IndexOf(')');
    while (rp < 0 || pcnt > 0)
    {
        NextLine();
        if (sIn.IndexOf('(') > 0) pcnt++;
        rp = sIn.IndexOf(')');
        if (rp >= 0)
        {
            pcnt--;
            sRel = sRel + sIn.Substring(0, ++rp);
        }
        else sRel = sRel + sIn;
    }
    return sRel;
}

//  prfx, vbl, op, next_pri, op or call, nxt prfx, nxt_vbl

string get_exp(string sst)
    {
      //  int vse = 0;
        while ((sst == null) || (sst.IndexOf(';') < 0) || sst.IndexOf("begin") >= 0 && sst.IndexOf("end") < 0)
      //  while ((sIn.IndexOf(';') < 0) && (sIn.IndexOf("end") < 0))
        {
            NextLine();
                sst += " " + sIn;
                if (sst.Trim() == "endmodule") break;
        }
        return sst;
    }

//string get_bexp(string sst)
//{
//    while ((sIn.IndexOf("end") < 0))
//    {
//        NextLine();
//        sst += " " + sIn;
//    };
//    return sst;
//}


List<stk_ty> get_opq(string opT)
{
    List<stk_ty> opL = new List<stk_ty>();
    
    string[] qops;
    string op = "";
    if (opT.IndexOf('?') >= 0)
    {
        iL.Items.Add(vexp);
        return opL;
    }
    int l = 0, bb = 0, be = 0, oi = 0;
    qops = opT.Split(wsp, StringSplitOptions.RemoveEmptyEntries);
    opT = String.Concat(qops);
    if(opT.IndexOf(';') > 0)
        opT = opT.Substring(0, opT.IndexOf(';'));
    stk_ty qv = new stk_ty();
    while (opT.Length > 0)
    {
        oi = opT.IndexOfAny(copers1);
        if (oi == 0)
        {
            qv.prfx = opT[0];
            opT = opT.Substring(1);
            oi = opT.IndexOfAny(copers1);
        }
        bb = opT.IndexOf('[');
        be = opT.IndexOf(']');
        if (char.IsDigit(opT[0]))
        {
            qv.v_nm = new vMod.vvbl();
            qv.typ = '#';
            if (oi > 0)
            {
              //  qv.v_nm.val = Convert.ToUInt32(opT.Substring(0, oi).Trim());
                //   nparm.val = Convert.ToUInt32(p.Substring(eix).Trim());
              //  bool ok = UInt32.TryParse(p.Substring(eix).Trim(), out nparm.val);
                uint num;
                if (UInt32.TryParse(opT.Substring(0, oi).Trim(), out num) == false)
                    MessageBox.Show("Convert error" + lineno);
                qv.v_nm.val = num;
                opT = opT.Substring(oi + 1);
            }
            else
            {
                // qv.v_nm.val = Convert.ToUInt32(opT.Trim()); uint num;
                uint num;
                if (UInt32.TryParse(opT.Trim(), out num) == false)
                    MessageBox.Show("Convert error" + lineno);
                qv.v_nm.val = num;
                opT = "";
            }
        }
        else
        {
          //  opT = opT.Substring(1);
            oi = opT.IndexOfAny(copers1);
        }
        //  }
        if (bb > 0)
        {
            if ((oi = opT.Substring(0, bb).IndexOfAny(copers1)) > 0)
            {
                qv.v_nm = get_vbl(opT.Substring(0, oi));  //, out qv.ix);
                qv.oper = char.ToString(opT[oi]);
                opT = opT.Substring(oi + 1);
            }
            else
            {
                qv.v_nm = get_vbl(opT.Substring(0, bb));  //, out qv.ix);
                qv.oper = char.ToString(opT[be + 1]);
                opT = opT.Substring(be + 1);
            }
        }
        else if (oi >= 0)
        {
            if (char.IsDigit(opT[0]))
            {
                qv.v_nm = new vMod.vvbl();
                qv.typ = '#';
                //  qv.v_nm.val = Convert.ToUInt32(opT);
                uint num;
                if (UInt32.TryParse(opT.Substring(0, oi).Trim(), out num) == false)
                    MessageBox.Show("Convert error" + lineno);
                qv.v_nm.val = num;
              //  uint num;
                //   opT = "";
            }
            else
            {
                if (oi == 0)
                    qv.v_nm = get_vbl(opT.Substring(1).Trim());
                else
                    qv.v_nm = get_vbl(opT.Substring(0, oi).Trim());
                if (qv.v_nm == null)
                    MessageBox.Show("opq cannot find " + opT + lineno);
                qv.oper = char.ToString(opT[oi]);
                opT = opT.Substring(oi + 1);
                if ((oi = opT.IndexOfAny(copers1)) == 0)
                {
                    qv.oper += char.ToString(opT[oi]);
                    opT = opT.Substring(oi + 1);
                }
            }
            //}
            // }
            //    else
            //        qv.v_nm = get_vbl(opT.Trim());
            ////    qv.v_nm = get_vbl(opT);  //.Substring(0, opT.IndexOf(';')));  //, out qv.ix);
            //    opT = "";
        }
        else
        {
        //    //         qv.prfx = opT[0];
        //    if (char.IsDigit(opT[0]))
        //    {
        //        qv.v_nm = new vMod.vvbl();
        //        qv.typ = '#';
        //        qv.v_nm.val = Convert.ToUInt32(opT);
        //        opT = "";
        //    }
        //    else
        //    {
            if(qv.v_nm == null)
            qv.v_nm = get_vbl(opT.Trim());
        opT = "";
        //        //       opT = opT.Substring(1);
        //        //        oi = opT.IndexOfAny(copers1);
        //    }
        //    //qv.v_nm = get_vbl(opT.Trim());
        //    //opT = "";
        }
        opL.Add(qv);
        if (opT.Length > 0)
        {
            qv.nxt = new stk_ty();
            qv = qv.nxt;
        }
   }
     
    return opL;

}


string always_events(ref vMod.valways va)  //, int atx )
{

    int lpx, rpx; 
    string[] nbScan;
            lpx = vexp.IndexOf('(');
            if (lpx < 0)   
            {   //  not event driven
                if (vexp.IndexOf('#') < 0)
                {
                    MessageBox.Show("events syntax");
                    return "error";
                }
                else  // delay driven
                {
                    sScan = vexp.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                    vMod.vvbl y;
                    if (sScan[0] == "begin")
                        do
                        {
                            sScan = sScan[1].Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                            tb dly = new tb();
                            //    dly.bv1 = Convert.ToUInt32(sScan[0].Substring(1));
                            uint num;
                            if (UInt32.TryParse(sScan[0].Substring(1).Trim(), out num) == false)
                                MessageBox.Show("Convert error" + lineno);
                            dly.bv1 = num;
                            dly.bv2 = dly.bv1;
                            string vnm =sScan[1].Substring(0, sScan[1].IndexOf('=') - 1).Trim(); //, out dly.dvbl);
                           
                                          //foreach (vMod.vvbl v in vSys.mvbls)
                                              {
                                                  y = get_vbl(vnm);
                                                 // if (v.name == vnm)
                                                  {
                                                      dly.dvbl = y;
                                                  //    y = get_vbl(vnm);
                                                  //    y = v;
                                                  //    break;
                                                  }
                                              }
                                              if (dly.dvbl.name != vnm)
                                                  MessageBox.Show("Cannot find initial vbl");
                            dly.name = dly.dvbl.name;
                            clk = get_opq(sScan[1].Substring(sScan[1].IndexOf('=') + 1));
                            int xi = 0;
                            dly.val = vb.vv( 0, clk[0]);
                            Addevent(ref dly);
                            //    NextLine();
                            //   token = vtok();
                            vexp = vexp.Substring(vexp.IndexOf(';') + 1).Trim();
                            if (vexp.Trim() == "end" || vexp.Trim().Length <= 0)
                                break;
                        } while (vexp.Trim() != "end");
                    else
                    {
                        tb dly = new tb();
                        //  dly.bv1 = Convert.ToUInt32(sScan[0].Substring(1));
                        uint num;
                        if (UInt32.TryParse(sScan[0].Substring(1).Trim(), out num) == false)
                            MessageBox.Show("Convert error" + lineno);
                        dly.bv1 = num;
                        dly.bv2 = dly.bv1;
                        dly.dvbl = get_vbl(sScan[1].Substring(0, sScan[1].IndexOf('=') - 1).Trim()); //, out dly.dvbl);
                        dly.name = dly.dvbl.name;
                        if (dly.dvbl.val == 0)
                            dly.dvbl.val = 1;
                        else
                            dly.dvbl.val = 0;
                        Addevent(ref dly);
                        NextLine();
                    }
                }
            }
            else   // (event) driven
            {
                if ((rpx = vexp.IndexOf(')')) < 0)
                {
                    MessageBox.Show("always event syntax error");
                }
                sScan = vexp.Substring(++lpx, rpx - lpx).Split(wsp);
                    for (int evs = 0; evs < sScan.Length; ) 
                    {
                        string ev = sScan[evs];
                        switch (sScan[evs])
                        {
                            case "posedge":
                                iL.Items.Add(sScan[evs++]);
                                string vvix = sScan[evs];
                                vMod.vvbl nw = get_vbl(vvix);
                                vMod.vvbl nws = null;
                                foreach (vMod.input ii in newMod.inputs)
                                    if (ii.name == nw.name)
                                        nws = ii.mOut.vbl;
                                if (nw.pe == null) nw.pe = new List<vMod.valways>();
                                nw.pe.Add(va);
                                if (nws.pe == null) nws.pe = new List<vMod.valways>();
                                nws.pe.Add(va);
                                iL.Items.Add(sScan[evs++]);
                                break;
                            case "negedge":
                                iL.Items.Add(sScan[evs++]);
                                vvix = sScan[evs];
                                nw = get_vbl(vvix);
                                if (nw.ne == null) nw.ne = new List<vMod.valways>();
                                nw.ne.Add(va);
                                iL.Items.Add(sScan[evs++]);
                                break;
                            case "*" :  // list all vbls
                                //iL.Items.Add(sScan[evs++]);
                                //vvix = sScan[evs];
                                //nw = get_vbl(vvix);
                                //if (nw.awL == null) nw.awL = new List<vMod.valways>();
                                //nw.awL.Add(va);
                                iL.Items.Add(sScan[evs++]);
                                break;
                            default:
                                {
                              //      MessageBox.Show("always " + sScan[evs]);
                                    iL.Items.Add(sScan[evs]);
                                    vvix = sScan[evs];
                                    nw = get_vbl(vvix);
                                    if (nw.awL == null) nw.awL = new List<vMod.valways>();
                                    nw.awL.Add(va);
                                    iL.Items.Add(sScan[evs++]);
                                }
                                break;
                        } if (sScan.Length > evs)
                            if (sScan[evs] == "or")
                            {
                                iL.Items.Add(" or ");
                                evs++;
                                continue;
                            }

                    }  //  end of for edges
                        //if (evs < sScan.Length)
                        //{
                        //    if ((sScan[evs].IndexOf(')')) >= 0)
                  //  vexp = get_exp(vexp.Substring(rpx + 1));
                    vexp = vexp.Substring(rpx + 1);
                        nbScan = vexp.Split(wsp, 2, StringSplitOptions.RemoveEmptyEntries);
                        token = vxtok(nbScan);
                        if (token == "begin")  // block
                        {
                            int bct = 1;
                            if (nbScan.Length > 1)
                                nbScan = nbScan[1].Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string sa in nbScan)
                            {
                                string[] nnbScan = sa.Split(new char[] { '<', '=', '(' }, StringSplitOptions.RemoveEmptyEntries);
                                token = nnbScan[0].Trim();
                                //if (token != "if")
                                //    valS.fif = null;
                                vS();
                            }
                         
                        }
                        else  // not a block:
                        {

                            //if (token != "if")
                            //if (va.fif == null)
                            //    va.fif = new vMod.vif(); ;
                            vS();
                            if (valS.fif != null && valS.fif.condL == null)
                                valS.fif = null;
                            return vexp =  vexp.Substring(vexp.IndexOf(';') + 1);
                        }
                        return vexp.Substring(vexp.IndexOf(')') + 1).Trim();
              //  }
           //     else
                {  // no spaces in events ...  parse "or"  ??
                    return vexp.Substring(vexp.IndexOf(')') + 1).Trim(); ;
                }
            }
    return "";
}


//vMod.vwire get_wire(string wire_nm)  //, out vMod.vvbl rv )
//{
//    if (wire_nm.IndexOf(';') > 0)
//        wire_nm = wire_nm.Substring(0, wire_nm.IndexOf(';'));
//    foreach (vMod.vwire ne in newMod.vwires)
//    {
//        if (ne.name == wire_nm)
//        {
//            return ne;
//        }
//    }
//    return null;
//}

//vMod.vwire add_wire(string wire_nm)
//    {
//        if (wire_nm.IndexOf(';') > 0)
//            wire_nm = wire_nm.Substring(0, wire_nm.IndexOf(';'));
//        foreach (vMod.vwire ne in newMod.vwires)
//        {
//            if (ne.name == wire_nm)
//            {
//                return ne;
//            }
//        }
//    vMod.vwire nv = new vMod.vwire();
//    nv.name = wire_nm;
//    newMod.vwires.Add(nv);
//    return nv;
//}

vMod.vvbl get_vbl(string vbl_nm)  //, out vMod.vvbl rv )
{
    if (vbl_nm.IndexOf(';') > 0)
        vbl_nm = vbl_nm.Substring(0, vbl_nm.IndexOf(';'));
    foreach (vMod.vvbl ne in newMod.mvbls)
    {
        if (ne.name == vbl_nm)
        {
         //   rv = ne;
            return ne;
        }
    }
    return null;
}

vMod.vvbl add_vvbl(string vbl_nm)
{
    if (vbl_nm.IndexOf(';') > 0)
        vbl_nm = vbl_nm.Substring(0, vbl_nm.IndexOf(';'));
    foreach (vMod.vvbl ne in newMod.mvbls)
    {
        if (ne.name == vbl_nm)
        {
            //   rv = ne;
            return ne;
        }
    }
    vMod.vvbl nv = new vMod.vvbl();
    nv.name = vbl_nm;
    
    newMod.mvbls.Add(nv);
    return nv;
}
 
public vMod nm_tbl(string nm_str)
{
    if(vSys.modList == null) vSys.modList = new List<vMod>();
    foreach (vMod ne in vSys.modList)  // see if module was already created
    {
        if (ne.name == nm_str)
            return ne;
    }
    vMod nnt = new vMod();  //create new module
   // nnt. = vpo2;
    nnt.ix = vSys.modList.Count;
    vSys.modList.Add(nnt);
    nnt.name = nm_str;
    return nnt;
}


private string NextLine()
{
    string in_ln;
    string sIn1;
    for (in_ln = sr_in.ReadLine(); !sr_in.EndOfStream; in_ln = sr_in.ReadLine())
    {
        lineno++;
        int l = 0;
        sIn1 = in_ln;
        if (in_ln == "" | (l = in_ln.IndexOf("//")) == 0) continue;
        if (l > 0) sIn1 = in_ln.Substring(0, l);  // comment removed
        sIn = sIn1.Trim();
        sScan = sIn.Split(new Char[] { }, 2, StringSplitOptions.RemoveEmptyEntries);
        if(sScan.Length == 0) continue;
        return sScan[0];
    }
    if (! end_flag)
    MessageBox.Show(fs_in.Name, "unexpected end_of_file");

    if(MessageBox.Show("Run Sim Step?", "", MessageBoxButtons.YesNo) == DialogResult.Yes) SimStep();
    return "";
}

public void SimStep()
{
    int ivv = 0;
    foreach (vMod vm in vSys.modList)
    {
    //    if(vm.inputs != null)
    //    foreach (vMod.input vmi in vm.inputs)
    //    {
    //       if (vmi.vbl == null)
    //       {
    //           if (vmi.mOut.vbl == null)
    //           {
    //               MessageBox.Show("missing connect");
    //               return;
    //               }
    //           else
    //           vmi.vbl = vmi.mOut.vbl;
    //       }
            
    //    }
    }
    nbL.Clear();
    //bL.RemoveAll();
    long seek_pos;
    for (int ex = 0, el = events.Count ; events.Count > 0; ex++)//el = events.Count; ex < el; ex++)
    {  //  process events that occur at this time
        //if (events[0].bv1 != rn_tm)
        //    break;
        tb pev = new tb();
        pev = events[0];
        events.RemoveAt(0);
        rn_tm = pev.bv1;
        if (pev.dvbl != null)
            pev.name =
                pev.dvbl.name;
     //   if (pev.dvbl.pe != null)
            //{  //  do each always, select pe or ne

            //}
            //Type t = pev.GetType();
            //switch (pev.GetType().ToString())
            //{
            //    case "tb":
            //        break;
            //}
            pev.dvbl.val = pev.val;

            seek_pos = scn_log.BaseStream.Position;
            scn_log.Write(String.Format("{0} {1:8} {2} {3} {4} {5} {6} {7} {8,4:X} {9}",
                pev.val != 0 ? "set" : "clr",
    rn_tm, pev.name, pev.nm_typ, pev.ix, pev.bv1, pev.bv2, pev.val, pev.val, pev.seek_ch));
            scn_log.WriteLine();
            pev.seek_ch = seek_pos;
            pev.sc = pev.val;
            if (pev.dvbl == null)
            {
                pev.dvbl = new vMod.vvbl();
                pev.dvbl.name = pev.name;
             //   pev.dvbl.val = pev.val;
            }
            scan(ref pev);
            //  pev.dvbl.scn_ix

        if (pev.bv2 > 0)
        {
            pev.bv1 += pev.bv2;
            pev.dvbl.val = pev.val;
            int xi = 0;
            pev.val = vb.vv(0, clk[0]);
        //    pev.sc = pev.val;
            Addevent(ref pev);
        }
        
            if (pev.dvbl.pe != null)
            {
                if(pev.dvbl.val > 0)
                foreach (vMod.valways va in pev.dvbl.pe)
                    doays(va) ;
            }
            if (pev.dvbl.ne != null)
            {
                if (pev.dvbl.val == 0)
                foreach (vMod.valways va in pev.dvbl.ne)
                    doays(va);

                //if (va.fif.condL != null)
                //    va.fif.simif();
            }
        if (pev.dvbl.awL != null)
            {
                foreach (vMod.valways va in pev.dvbl.awL)
                    doays(va);
            }
        if (pev.dvbl.pe != null)
        {
            foreach (vMod.valways va in pev.dvbl.pe)
               if(va.nbU != null)
                   {
                       foreach (vMod.vvbl uv in va.nbU)
                       {
                        //   if (uv.lhs != uv.rhs)
                           {
                               uv.lhs = uv.rhs;
                               tb pev2 = new tb();
                               pev2.dvbl = uv;
                               pev2.val = uv.val;
                               pev2.sc = pev2.val;
                               scan(ref pev2);
                           }
                       }
                       va.nbU.Clear();
                   }
        }
        foreach(vMod vm in vSys.modList)
            foreach(vMod.vvbl vb in vm.mvbls)
            {
                if (vb.awL != null)
                    foreach (vMod.valways va in vb.awL)
                        doays(va);
            }
        if (pev.bv1 > rn_tm)
        {
        //    if (pev.dvbl.pe != null)
        //    {
        //        foreach (vMod.valways va in pev.dvbl.pe)
        //            va.doays() ;
        //    }
        }
//        rn_tm = pev.bv1;
        if (rn_tm > simtm) break;
        //if (pev.dvbl.awL != null)
        //{
        //    foreach (vMod.valways vt in pev.dvbl.awL)
        //    {
        //        if (vt.bL != null) foreach (vMod.vvbl bv in vt.bL)
        //            {
        //                bv.val = vb.vv(bv.opL, 0, ref ivv);
        //            }
        //    }
        //}
        //pev.dvbl.val = pev.val;
        //int xi = 0;
        //pev.val = vb.vv(clk, 0, ref xi);
    }
    byte mode = 0, pCode = 0;
    scn_out(ref mode, 0, iL, ref pCode);
    foreach (object o in vSys.sList)
    {
        if (o is vMod.valways)
        {

        }
        switch (o.GetType().ToString())
        {
            case "vif" :
            case "velse" :
            case "valways" :
                break;
            default:
                break;


        }
    }
}

public void doays(vMod.valways va)
{
    int ri = 0;
    if (va.nbU == null)
        va.nbU = new List<vMod.vvbl>();
    if (va.bL.Count != 0)
    {
        foreach (vMod.ops v in va.bL)
        {
            ri = 0;
            v.vr.lhs = vb.vv(0, v.opL[0]);
            {
                
                tb pev = new tb();
                pev.dvbl = v.vr;
                pev.val = pev.dvbl.val;
                if(v.vr.lhs != v.vr.val)
                scan(ref pev);
                if (v.vr.awL != null)
                    foreach (vMod.valways vaw in v.vr.awL)
                        doays(vaw);
            }
        }
    }
    else
        if (va.nbL.Count != 0)
        {
            foreach (vMod.ops v in va.nbL)
            {
                ri = 0;
                if(v.opL != null)
                v.vr.rhs = vb.vv(0, v.opL[0]);
                else v.vr.rhs = v.vr.vval;
                if(v.vr.lhs != v.vr.rhs)
                va.nbU.Add(v.vr);

                //}
            }
        
            //}
        }
    if (va.fif != null && va.fif.condL != null)
    {
        va.fif.simif(va.nbU);
        foreach (vMod.vvbl vv in va.nbU)
           // if (vv.lhs != vv.rhs)
            {
                vv.lhs = vv.rhs;

            }
            ;
    }
        
    //   else

}

public void scan(ref tb sx)   // tb* sx)
{
    int ln_tm = (int)rn_tm - (int)scn_tm;
    int scn_ix = (int)sx.dvbl.scn_ix;
    if (scn.Count > 0 &&  //sx.dvbl.scn_ix)
        //   {
        scn[(int)sx.dvbl.scn_ix].vbl.Equals(sx.dvbl))
        {
            s_p = scn[(int)sx.dvbl.scn_ix];
            if (s_p.tm == ln_tm)
            {
                MessageBox.Show(String.Format("{0} multiple state changes at time, {1}", sx.dvbl.name, rn_tm.ToString()));
                //               err_flg = 1;
                return;
            }
        }
        else 
    {
        s_p = new scrn();
        s_p.ix = (int)sx.ix;
        s_p.ln = new string('.', ln_tm);
        s_p.ln += (sx.sc > 0) ? "/" : "\\";  //  char.ToString((sx.sc > 0) ? '/' : '\\');
    //    s_p.ln += char.ToString((sx.sc > 0) ? '/' : '\\');
 //       s_p.ln = "\\My Documents\";
        s_p.tm = (int)rn_tm;
        vals vt = new vals();
        vt.time = rn_tm;
        vt.val = sx.val;
        sx.dvbl.scn_ix = scn.Count;
        s_p.vbl = sx.dvbl;
        s_p.vbl.scn_ix = scn.Count;
        s_p.trace = new List<vals>();
        s_p.trace.Add(vt);
        scn.Add(s_p);
        return;
    }
    //}
    //else
    //        if (s_p.tm == ln_tm)
    //        {
    //            MessageBox.Show(String.Format("{0} multiple state changes at time, {1}", sx.dvbl.name, rn_tm.ToString()));
    //            //               err_flg = 1;
    //            return;
    //        }
            if (s_p.ln.Length < ln_tm)
                s_p.ln = s_p.ln.PadRight(ln_tm, s_p.ln[s_p.ln.Length - 1] == '/' ? '+' : '_'); //  (char)45 : (char)46);

            s_p.ln += (sx.sc != 0 ? '/' : '\\');
            s_p.tm = ln_tm;
            vals vt2 = new vals();
            vt2.time = rn_tm; 
            vt2.val = sx.val;
            //if(s_p.trace == null)
            //    s_p.trace = new List<vals>();
            s_p.trace.Add(vt2);
            s_p.seek_pos = sx.seek_ch;
            scn[(int)sx.dvbl.scn_ix] = s_p;
            return ;
   //     }
  ////  }
  //  else s_p = new scrn();
  //  s_p.ix = (int)sx.ix;
  //  s_p.ln = new string('.', ln_tm);
  //  s_p.ln += char.ToString((sx.sc > 0) ? '/' : '\\');
  //  s_p.tm = ln_tm;
  //  sx.dvbl.scn_ix = scn.Count;
  //  s_p.vbl = sx.dvbl;
  //  s_p.vbl.scn_ix = scn.Count;
  //  scn.Add(s_p);

    return ;
}

char scn_out(ref byte mode, int ln_tm, ListBox pLBox, ref byte pCode)
{   // scn_out gens output via LBox
    int nssAr = 0;
    int units;
    int tens;
    int tens_ct;
    int length;
    String lbadd = "**Double click facility for last change then use P/B for prev change**";
    lB2.Items.Add(lbadd);
    nssAr++;
    //scn_tm = 99;
    tens = scn_tm % 100;
    units = tens % 10;
    tens /= 10;
    byte cTens = (byte)tens;
    String sLine;
    String sChar;
    int scn_tma = scn_tm;
    byte[] sCh = new byte[128];
    lbadd = "**Time**   ";
    lbadd += scn_tm;
    lB2.Items.Add(lbadd);
    nssAr++;
    sLine = "                    ";
    for (tens_ct = units, length = 0; length < 100; length++)
    {
        sChar = String.Format("{0}", tens);
        sLine += sChar;
        if (tens_ct++ == 9)
        {
            if (tens++ == 9) tens = 0;
            tens_ct = 0;
        }
    }
    lB2.Items.Add(sLine);
    sLine = "                    ";
    for (length = 0; length < 100; length++)
    {
        sChar = String.Format("{0}", units);
        sLine += sChar;
        if (units++ == 9) units = 0;
    }
    lB2.Items.Add(sLine);
    string lno;
    foreach (scrn so in scn)
    {
        lno = so.ln;
        if (lno.Length < ln_tm)
        {
            if (lno[lno.Length - 1] == '\\') lno += '.';
            if (lno[lno.Length - 1] == '/') lno += '-';
        }
        string lbs;
        if (so.vbl.name != null)
        {
            lbs = String.Format("{0}{1}", so.vbl.name.PadRight(20, '.'), lno.PadRight(ln_tm, lno[lno.Length - 1]));
            lB2.Items.Add(lbs);
        }
    }

    return 'l';
}


int initial(string dPath)   //(StreamReader sr_nms, StreamReader sr_v_stk, BinaryReader br_v_stk)
{
    string opath = dPath.Substring(0, dPath.IndexOf('.')) + "\\dump.txt";
    log_out = new FileStream("dump.txt", FileMode.OpenOrCreate);
    scn_log = new StreamWriter(log_out);
    scn_log.AutoFlush = true;
    log_in = new StreamReader(log_out);
    return (0);
}


public void action(int nSel, ref int nChar, int nTime)
// action traces back thru log file to find state change/value
{
    String sFac;
    int find_ix;
    int time_ix;
    int chg_tm = 0;
    char[] log_op = new char[4];
    char[] log_nm = new char[32];
    find_ix = nSel - 4;

    long seek_ptr = scn[find_ix].seek_pos;  // = prntx.seek_ch; ;
    string inlog;
    string[] sScan = new string[16];
    char[] sep = new char[1];
    sep[0] = ' '; 
    time_ix = scn_tm + nTime - 17;
    long seek_pos = log_in.BaseStream.Position;
    if (nChar == 0)
    {
        if (scn[find_ix].trace == null)
        {
            sFac = String.Format("No history is available for {0}", scn[find_ix].vbl.name);
            lB3.Items.Insert(0, sFac);
        }
        else
        {
            sFac = String.Format("{0} first changed at {1,4} val = 0x{2,4:X}", 
                scn[find_ix].vbl.name, scn[find_ix].trace[nChar].time,  scn[find_ix].trace[nChar++].val);
            lB3.Items.Insert(0, sFac);
        }
        //if (seek_ptr > 0)  val = 0x{2,4:X} 
        //{
        //    do
        //    {
        //        inlog = log_in.ReadLine();
        //        if (inlog != null)
        //        {
        //            sScan = inlog.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        //            chg_tm = Convert.ToInt32(sScan[1]);
        //            seek_ptr = Convert.ToInt64(sScan[9]);
        //        }
        //    } while (inlog != String.Empty & chg_tm > time_ix);
        //    if (inlog != String.Empty)
        //    {
        //        sScan = inlog.Split(sep, StringSplitOptions.RemoveEmptyEntries);
        //        chg_tm = Convert.ToInt32(sScan[1]);
        //        seek_ptr = Convert.ToInt64(sScan[9]);
        //    }
        //    if (chg_tm > time_ix)
        //    {
        //        sFac = String.Format("{0} first changed at {1,4} val = 0x{2,4:X} ", scn[find_ix].vbl.name, chg_tm,
        //            Convert.ToUInt16(sScan[7]));
        //    }
        //    else
        //    {
        //        string st = String.Format("{0:X}", Convert.ToUInt16(sScan[8]));

        //        sFac = String.Format("{0} last changed at {1,4} val = 0x", scn[find_ix].vbl.name, chg_tm);
        //        sFac += sScan[8];
        //    }
        //    lB3.Items.Insert(0, sFac);
        //}
        //else
        //{
        //    sFac = String.Format("No history is available for {0}", scn[find_ix].vbl.name);
        //    lB3.Items.Insert(0, sFac);
        //}
    }
    else
    {
        if(nChar < scn[find_ix].trace.Count)
        sFac = String.Format("{0} changed at {1,4} val = 0x{2,4:X} ", scn[find_ix].vbl.name, scn[find_ix].trace[nChar].time, scn[find_ix].trace[nChar++].val);
    //                    sScan[8]);
        else sFac = "no more changes";
                    lB3.Items.Insert(0, sFac);
    //    log_in.DiscardBufferedData();
    //    if (nChar == 1)
    //    {
    //        if (seek_ptr > 0)
    //        {
    //            log_in.DiscardBufferedData();
    //            log_in.BaseStream.Seek(seek_ptr, 0);
    //            inlog = log_in.ReadLine();
    //            if (inlog != null)
    //            {
    //                sScan = inlog.Split(sep, StringSplitOptions.RemoveEmptyEntries);
    //                chg_tm = Convert.ToInt32(sScan[1]);
    //                seek_ptr = Convert.ToInt64(sScan[9]);
    //                sFac = String.Format("{0} changed at {1,4} val = 0x{2} ", scn[find_ix].vbl.name, chg_tm,
    //                    sScan[8]);
    //                lB3.Items.Insert(0, sFac);
    //            }
    //        }
    //        else
    //        {
    //            sFac = String.Format("No more history is available for {0}", scn[find_ix].vbl.name);
    //            lB3.Items.Insert(0, sFac);
    //        }
    //    }

    }
    //scn_log.BaseStream.Position = seek_pos;

}
   

private void Addevent(ref tb x)
{
    int ix = 0;
    for (int ct = events.Count; ix < ct; ix++)
    {
        if (events[ix].bv1 > x.bv1)
            break;
    }
    {
        events.Insert(ix, x);
        return;
    }
    MessageBox.Show("Addevent Error");
}
ulong get_digits()
{
    //  xOp += sScan[xT].Length + 1;
    //if (sIn[xOp] == '.')  //sScan[0].Length] == '.')
    //{
    //    xOp++;
    //try
    //{
    //    if (sScan[xT].Length > 2)
    //    {
    //        if (sScan[xT][0] == '0' && sScan[xT][1] == 'x')
    //            return (UInt32.Parse(sScan[xT].Substring(2), NumberStyles.HexNumber));
    //        else return (Convert.ToUInt32(sScan[xT]));
    //    }
    //    else return (Convert.ToUInt32(sScan[xT]));
    //}
    //catch
    { MessageBox.Show("Invalid arg to get_digits"); }
    /*if (isdigit(c))
       {// num = num_str;
           dig_cnt = 0;
           if (c == '0')
            {
               // c = getc(fp);
               if (c == 'x')
                { *num++ = '0';
                    *num++ = 'x';
                    for (c = getc(fp); isxdigit(c); c = getc(fp) )
                       { *num++ = c;
                           dig_cnt++;
                       }
                }
               else
                for (; c == '0'; c = getc(fp) );
            }
           for (; isdigit(c); c = getc(fp) )
            { *num++ = c;
                dig_cnt++;
            }
           *num = '\0';
           if (strchr(spceq,c)) cd = more();
           return strtoul(num_str,&endnum,0);
       
       }
//		 cout << "invalid arg to get_digits";
		
MessageBox.Show ("invalid arg to get_digits");
     */
 //   err_cd = 19;
    return 0;
}


//public  uint vv( List<stk_ty> vL, int prec, ref int ox)
//{
    
//    v1 = vL[ox].v_nm.val;  // vvbl.val;  wire vvbl or vv(); 
//    if (vL[ox].prfx == '!')
//        if (v1 > 0) v1 = 0;
//        else v1 = 1;
//    if (vL.Count == 1)
//        return v1;
//        for (; ox < vL.Count; )  // ox++)
//        {
//            if ((op = vL[ox].oper) != null && (++ox < vL.Count))
//            {
//                //  bp = vprec.op; // get_prec(op);
//                // v2 = vval(vL[ox].ix);
//                //  np = vprec.vL[ox].oper;
//            }
//            //  return rslt(v1, op, v2)
//            if (op == ")" || nxtprec < prec) return v1;

//            switch (op)
//            {
//                case "&":
//                    //if ((nxtprec = (int)vprec.A) > prec)
//                    //    v1 &= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.A;
//                    v1 &= vval(vL[ox].v_nm.val);
//                    break;
//                case "|":
//                    //if ((nxtprec = (int)vprec.O) > prec)
//                    //    v1 |= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.O;
//                    v1 |= vval(vL[ox].v_nm.val);
//                    break;
//                case "^":
//                    //if ((nxtprec = (int)vprec.XO) > prec)
//                    //    v1 ^= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.XO;
//                    v1 ^= vval(vL[ox].v_nm.val);
//                    break;
//                case "+":
//                    //if ((nxtprec = (int)vprec.AS) > prec)
//                    //    v1 += vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.AS;
//                    v1 += vval(vL[ox].v_nm.val);
//                    break;
//                case "-":
//                    //if ((nxtprec = (int)vprec.AS) > prec)
//                    //    v1 -= vv(ref vL, nxtprec, ref ox);
//                    //else

//                    prec = (int)vprec.AS;
//                    v1 -= vval(vL[ox].v_nm.val);
//                    break;
//                case "~&":
//                    //if ((nxtprec = (int)vprec.NA) > prec)
//                    //    v1 = ~(v1 &= vv(ref vL, nxtprec, ref ox));
//                    //else
//                    prec = (int)vprec.NA;
//                    v1 = ~(v1 & vval(vL[ox].v_nm.val));
//                    break;
//                case "~|":
//                    //if ((nxtprec = (int)vprec.O) > prec)
//                    //    v1 = ~(v1 | vv(ref vL, nxtprec, ref ox));
//                    //else
//                    prec = (int)vprec.NO;
//                    v1 = ~(v1 | vval(vL[ox].v_nm.val));
//                    break;
//                case "^~":
//                    //if ((nxtprec = (int)vprec.XO) > prec)
//                    //    v1 = ~(v1 ^ vv(ref vL, nxtprec, ref ox));
//                    //else
//                    prec = (int)vprec.XO;
//                    v1 = ~(v1 ^ vval(vL[ox].v_nm.val));
//                    break;
//                case "&&":
//                    if ((v1 > 0) && (vval(vL[ox].v_nm.val) > 0))
//                        return 1;
//                    else
//                        return 0;
//                case "||":
//                    if ((v1 > 0) || (vval(vL[ox].v_nm.val) > 0))
//                        return 1;
//                    else
//                        return 0;
//                case "<":
//                    if (v1 < vval(vL[ox].v_nm.val))
//                        return 1;
//                    else
//                        return 0;
//                case "<=":
//                    if (v1 <= vval(vL[ox].v_nm.val))
//                        return 1;
//                    else
//                        return 0;
//                //case "=":
//                //    if (v1 == v2)
//                //        return 1;
//                //    else
//                //        return 0;
//                case "==":
//                    if (v1 == vval(vL[ox].v_nm.val))
//                        return 1;
//                    else
//                        return 0;
//                case ">":
//                    if (v1 > vval(vL[ox].v_nm.val))
//                        return 1;
//                    break;
//                case ">=":
//                    if (v1 >= vval(vL[ox].v_nm.val))
//                        return 1;
//                    else
//                        return 0;
//                case "*":
//                    //if ((nxtprec = (int)vprec.MD) > prec)
//                    //    v1 *= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.MD;
//                    v1 *= vval(vL[ox].v_nm.val);
//                    break;
//                case "/":
//                    //if ((nxtprec = (int)vprec.MD) > prec)
//                    //    v1 /= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.MD;
//                    v1 /= vval(vL[ox].v_nm.val);
//                    break;
//                case "%":
//                    //if ((nxtprec = (int)vprec.MD) > prec)
//                    //    v1 %= vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.MD;
//                    v1 %= vval(vL[ox].v_nm.val);
//                    break;
//                case "<<":
//                    //if ((nxtprec = (int)vprec.LRS) > prec)
//                    //    v1 <<= (int)vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.LRS;
//                    v1 <<= (int)vval(vL[ox].v_nm.val);
//                    break;
//                case ">>":
//                    //if ((nxtprec = (int)vprec.LRS) > prec)
//                    //    v1 >>= (int)vv(ref vL, nxtprec, ref ox);
//                    //else
//                    prec = (int)vprec.LRS;
//                    v1 >>= (int)vval(vL[ox].v_nm.val);
//                    break;
//                case "===":
//                    break;
//                case "!==":
//                    break;
//                default:
//                    MessageBox.Show("Missed op " + ox);
//                    break;
//            }
//            return 0;
//        }

//    return v1;
	   

//    return 0Xffffffff;
//}

//uint vval(uint vix)
//{
//    // if(oper == "(" || (nxtprec > prec) return vv()
//    // else return .val
//    return 0;
//}
        //public class ops : IEnumerator
        //{
        //    public stk_ty[] opa;

        //    int position = -1;

        //    public ops(List<stk_ty> oL)
        //    {
        //        opa = oL.ToArray();
        //    }

        //    public bool MoveNext()
        //    {
        //        position++;
        //        return (position < opa.Length);
        //    }

        //    public void Reset()
        //    {
        //        position = -1;
        //    }

        //   public  object  Current
        //    {
        //        get
        //        {
        //            try
        //            {
        //                return opa[position];
        //            }
        //            catch (IndexOutOfRangeException)
        //            {
        //                throw new InvalidOperationException();
        //            }
        //        }
        //    }

        //}

    }  //  end vParse
}  //  end namespace
